<?php
/**
 * 枚举类型表的操作
 * @author Ju <hipop@126.com>
 *
 */
class Application_Model_DbTable_Enumdata extends Application_Model_DbTable_Base
{
    protected $_name = 'enumdata';
    
    /**
     * 构造函数
     * @param string $talbename
     */
    public function __construct($talbename = NULL){
    	$this->_name = $talbename;
    	parent::__construct();
    }
    
    /**
     * 设置表名
     * @param string $tablename
     */
    public function setName($tablename){
    	$this->_name = $this->_prefix . $tablename;
    	return $this;
    }
    
    /**
     * 枚举数据的列表
     * var array
     */
    public $ENUMARR = array(
    	"container_size" 	=> "货柜型号",
    	"region" 			=> "国家和地区",
    	"trade" 			=> "行业分类",
    	"pay_rule" 			=> "支付方式",
    	"trade_mode" 		=> "贸易方式",
    	"transport_batches" => "分批装运",
    	"transport_transshipment" => "转船运输",
    	"transport_mode" 	=> "运输方式",
    	"price_rule" 		=> "价格术语",
    	"customer_degree"	=> "客户等级",
    	"customer_type"		=> "客户类型"
    );
    
    /**
     * 帮助生成表单的下拉列表
     * @param Application_Form_Base $formM 被追加数据的form
     * @param string $elementname 控件名
     * @param string $column 字段名称
     * @return Application_Form_Base
     */
	public static function setMultiOptions(Application_Form_Base $formM,$elementname,$table = NULL,$column = "name_cn"){
		//添加下拉列表数据
		$table 		= $table?$table:$elementname;
		$elementE 	= $formM->getElement($elementname);
		if($elementE){
			$elementM = new Application_Model_DbTable_Enumdata($table);
			$elementL = $elementM->fetchAll();
			$elementD = array();
			foreach ($elementL as $element){
				$elementD[$element["id"]] = $element[$column] ;
			}
			$elementE->setMultiOptions($elementD);
		}
		return $formM;
	}
	
	/**
	 * 设置所有枚举的控件下拉列表
	 * @param Application_Form_Base $formM
	 */
	public function setMultiOptionAll(Application_Form_Base $formM){
		foreach ($this->ENUMARR as $k => $e){
			$formM = self::setMultiOptions($formM,$k,$k);
		}
		return $formM;
	}
	
	/**
	 * 获取id->name的键值对，
	 * @param string $lang 缺省为cn 可选为en
	 */
	public function getKvIdName($lang="cn"){
		$rows   = $this->fetchAll();
		$data   = array();
		$culumn = "name_".$lang;
		foreach ($rows as $r){
			$data[$r->id] = $r->$culumn;
		}
		return $data;
	}
}